package pl.softech.engine3d;

import java.awt.Color;
import java.awt.DisplayMode;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Window;

import javax.swing.ImageIcon;

import pl.softech.engine3d.screen.ScreenManager;

public abstract class GameCore {

	protected static final DisplayMode[] MID_RES_MODES = {
        new DisplayMode(800, 600, 16, 0),
        new DisplayMode(800, 600, 32, 0),
        new DisplayMode(800, 600, 24, 0),
        new DisplayMode(640, 480, 16, 0),
        new DisplayMode(640, 480, 32, 0),
        new DisplayMode(640, 480, 24, 0),
        new DisplayMode(1024, 768, 16, 0),
        new DisplayMode(1024, 768, 32, 0),
        new DisplayMode(1024, 768, 24, 0),
    };

    protected static final DisplayMode[] LOW_RES_MODES = {
        new DisplayMode(640, 480, 16, 0),
        new DisplayMode(640, 480, 32, 0),
        new DisplayMode(640, 480, 24, 0),
        new DisplayMode(800, 600, 16, 0),
        new DisplayMode(800, 600, 32, 0),
        new DisplayMode(800, 600, 24, 0),
        new DisplayMode(1024, 768, 16, 0),
        new DisplayMode(1024, 768, 32, 0),
        new DisplayMode(1024, 768, 24, 0),
    };

    protected static final DisplayMode[] VERY_LOW_RES_MODES = {
        new DisplayMode(320, 240, 16, 0),
        new DisplayMode(400, 300, 16, 0),
        new DisplayMode(512, 384, 16, 0),
        new DisplayMode(640, 480, 16, 0),
        new DisplayMode(800, 600, 16, 0),
    };
	
	protected static final DisplayMode[] POSSIBLE_MODES = {
//		new DisplayMode(800, 600, 32, DisplayMode.REFRESH_RATE_UNKNOWN),
//		new DisplayMode(800, 600, 24, DisplayMode.REFRESH_RATE_UNKNOWN),
//		new DisplayMode(800, 600, 16, DisplayMode.REFRESH_RATE_UNKNOWN),
//		new DisplayMode(1152, 864, 32, DisplayMode.REFRESH_RATE_UNKNOWN),
//		new DisplayMode(1152, 864, 24, DisplayMode.REFRESH_RATE_UNKNOWN),
//		new DisplayMode(1152, 864, 16, DisplayMode.REFRESH_RATE_UNKNOWN),
		new DisplayMode(1680, 1050, 32, DisplayMode.REFRESH_RATE_UNKNOWN),
		new DisplayMode(1680, 1050, 24, DisplayMode.REFRESH_RATE_UNKNOWN),
		new DisplayMode(1680, 1050, 16, DisplayMode.REFRESH_RATE_UNKNOWN),
	};
	
	protected static final int DEFAULT_FONT_SIZE = 12;
	
	protected int fontSize = DEFAULT_FONT_SIZE;
	
	private boolean isRunning;
	protected ScreenManager screen;
	
	public void stop() { isRunning = false; }
	
	protected void run() {
		try {
			init();
			gameLoop();
		} finally {
			screen.restoreScreen();
		}
	}
	
	protected void init() {
		screen = new ScreenManager();
		DisplayMode displayMode = screen.findFirstCompatibleMode(POSSIBLE_MODES);
		if(displayMode == null)
			displayMode = screen.getCurrentDisplayMode();
		screen.setFullScreen(displayMode);
		
		Window window = screen.getFullScreenWindow();
		window.setFont(new Font("Dialog", Font.PLAIN, fontSize));
		window.setBackground(Color.BLUE);
		window.setForeground(Color.WHITE);
		
		isRunning = true;
	}
	
	public Image loadImage(String fileName) { 
		return new ImageIcon(fileName).getImage(); 
	} 
	
	protected void gameLoop() {
		long startTime = System.currentTimeMillis();
		long currTime = startTime;
		
		while(isRunning) {
			long elapsedTime = System.currentTimeMillis() - currTime;
			currTime += elapsedTime;
			
			//Aktualizacja
			update(elapsedTime);
			
			//Rysowanie
			Graphics2D g = screen.getGraphics();
			draw(g);
			g.dispose();
			screen.update();
			
			try {
				Thread.sleep(20);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
	
	public void update(long elapsedTime) {}
	
	public abstract void draw(Graphics2D g); 
}
